﻿using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace SenileCoderMvc.UnitTests
{
    /// <summary>
    /// Route testing helper. Taken from http://bradwilson.typepad.com/blog/2010/07/testing-routing-and-url-generation-in-aspnet-mvc.html
    /// </summary>
    public static class UrlHelperFactory
    {
        public static UrlHelper GetUrlHelper(string appPath = "/", RouteCollection routes = null)
        {
            if (routes == null)
            {
                routes = new RouteCollection();
                MvcApplication.RegisterRoutes(routes);
            }

            HttpContextBase httpContext = new StubHttpContextForRouting(appPath);
            var routeData = new RouteData();
            routeData.Values.Add("controller", "defaultcontroller");
            routeData.Values.Add("action", "defaultaction");
            var requestContext = new RequestContext(httpContext, routeData);
            var helper = new UrlHelper(requestContext, routes);
            return helper;
        }
    }

    /// <summary>
    /// Route testing helper. Taken from http://bradwilson.typepad.com/blog/2010/07/testing-routing-and-url-generation-in-aspnet-mvc.html
    /// </summary>
    public class StubHttpContextForRouting : HttpContextBase
    {
        readonly StubHttpRequestForRouting request;
        readonly StubHttpResponseForRouting response;

        public StubHttpContextForRouting(string appPath = "/", string requestUrl = "~/")
        {
            request = new StubHttpRequestForRouting(appPath, requestUrl);
            response = new StubHttpResponseForRouting();
        }

        public override HttpRequestBase Request
        {
            get { return request; }
        }

        public override HttpResponseBase Response
        {
            get { return response; }
        }
    }

    /// <summary>
    /// Route testing helper. Taken from http://bradwilson.typepad.com/blog/2010/07/testing-routing-and-url-generation-in-aspnet-mvc.html
    /// </summary>
    public class StubHttpRequestForRouting : HttpRequestBase
    {
        readonly string appPath;
        readonly string requestUrl;

        public StubHttpRequestForRouting(string appPath, string requestUrl)
        {
            this.appPath = appPath;
            this.requestUrl = requestUrl;
        }

        public override string ApplicationPath
        {
            get { return appPath; }
        }

        public override string AppRelativeCurrentExecutionFilePath
        {
            get { return requestUrl; }
        }

        public override string PathInfo
        {
            get { return ""; }
        }

        public override NameValueCollection ServerVariables
        {
            get { return new NameValueCollection(); }
        }
    }

    /// <summary>
    /// Route testing helper. Taken from http://bradwilson.typepad.com/blog/2010/07/testing-routing-and-url-generation-in-aspnet-mvc.html
    /// </summary>
    public class StubHttpResponseForRouting : HttpResponseBase
    {
        public override string ApplyAppPathModifier(string virtualPath)
        {
            return virtualPath;
        }
    }
}